﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_edproduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from product where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;
        SqlParameter paramUserName = new SqlParameter("@id", SqlDbType.BigInt);
        paramUserName.Value = Request.Params["id"];
        mySqlSelect.Parameters.Add(paramUserName);
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                //info.Text = getCat((Int64)rd.GetSqlInt64(3).ToSqlInt32()).ToString();
                cattype.SelectedValue = getCat((Int64)rd.GetSqlInt64(3).ToSqlInt32()).ToString();
                protype.SelectedValue = rd.GetSqlInt64(3).ToString();
                des.Text = rd.GetSqlString(4).ToString();
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    public Int64 getCat(Int64 id)
    {
        Int64 rs = 0;
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from producttype where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;
        SqlParameter paramUserName = new SqlParameter("@id", SqlDbType.BigInt);
        paramUserName.Value = id;
        mySqlSelect.Parameters.Add(paramUserName);
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                rs = (Int64) rd.GetSqlInt64(1).ToSqlInt32();
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
        return rs;
    }


    protected void addbt_Click(object sender, EventArgs e)
    {
        info.Text = "";
        try
        {
            connection con = new connection();
            SqlConnection cn = con.connect();
            SqlCommand mySqlSelect = new SqlCommand("update product set logoUrl =  @logo, imageUrl = @img, producttypeid = @protype, description = @des where id = @id", cn);
            mySqlSelect.CommandType = CommandType.Text;

            SqlParameter desc = new SqlParameter("@des", SqlDbType.NVarChar, 50);
            desc.Value = des.Text;

            SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
            id.Value = Request.QueryString["id"];

            SqlParameter ptype = new SqlParameter("@protype", SqlDbType.BigInt);
            ptype.Value = protype.SelectedValue;
            //insert logo
            SqlParameter logot = new SqlParameter("@logo", SqlDbType.NVarChar, 150);

            String regexx = "";
            if (logo.PostedFile.ContentLength > 2097151)
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
                return;
            }
            else
            {
                regexx = System.IO.Path.GetExtension(logo.PostedFile.FileName.ToLower().ToString());
                String picPath = "~/image/" + Request.QueryString["id"] + regexx;
                if (regexx == ".bmp" || regexx == ".jpg" || regexx == ".gif" || regexx == ".png")
                {
                    logo.SaveAs(Server.MapPath(picPath));
                }
                else if (regexx != "")
                {
                    info.Visible = true;
                    info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
                }
            }
            if (regexx == "")
            {
                logot.Value = null;
            }
            else
            {
                logot.Value = logo.PostedFile.FileName;
            }

            //insert image
            SqlParameter url = new SqlParameter("@img", SqlDbType.NVarChar, 150);

            String regex = "";
            if (img.PostedFile.ContentLength > 2097151)
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
                return;
            }
            else
            {
                regex = System.IO.Path.GetExtension(img.PostedFile.FileName.ToLower().ToString());
                String picPath = "~/image/" + img.PostedFile.FileName;
                if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
                {
                    img.SaveAs(Server.MapPath(picPath));
                }
                else if (regex != "")
                {
                    info.Visible = true;
                    info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
                }
            }
            if (regex == "")
            {
                url.Value = null;
            }
            else
            {
                url.Value = img.PostedFile.FileName;
            }



            if (regex == "" && regexx == "")
            {
                mySqlSelect = new SqlCommand("update product set producttypeid = @protype, description = @des where id = @id", cn);
                mySqlSelect.Parameters.Add(desc);
                mySqlSelect.Parameters.Add(id);
                mySqlSelect.Parameters.Add(ptype);
            }
            else if (regex == "" && regexx != "")
            {
                mySqlSelect = new SqlCommand("update product set logoUrl =  @logo, producttypeid = @protype, description = @des where id = @id", cn);
                mySqlSelect.Parameters.Add(desc);
                mySqlSelect.Parameters.Add(id);
                mySqlSelect.Parameters.Add(ptype);
                mySqlSelect.Parameters.Add(logot);
            }
            else if (regex != "" && regexx == "")
            {
                mySqlSelect = new SqlCommand("update product set imageUrl = @img, producttypeid = @protype, description = @des where id = @id", cn);
                mySqlSelect.Parameters.Add(desc);
                mySqlSelect.Parameters.Add(id);
                mySqlSelect.Parameters.Add(ptype);
                mySqlSelect.Parameters.Add(url);
            }
            else
            {
                mySqlSelect.Parameters.Add(desc);
                mySqlSelect.Parameters.Add(id);
                mySqlSelect.Parameters.Add(ptype);
                mySqlSelect.Parameters.Add(url);
                mySqlSelect.Parameters.Add(logot);
            }

            mySqlSelect.ExecuteNonQuery();
            cn.Close();
            info.Visible = true;
            info.Text = "Cập nhật thành công";
        }
        catch (Exception ex)
        {
            info.Text = "Có lỗi trong quá trình nhập liệu";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=qlproduct");
    }
}